<?php


namespace app\sys\model;


use think\Db;
use think\Model;

class Dict extends Model
{
    protected $autoWriteTimestamp = true;

    public static function add()
    {
        
    }

    public static function list($cate)
    {
        return Dict::field("label,value")->where("cate",$cate)->select();
    }

    /**
     * @param $cate
     * @param $label
     * @return mixed
     */
    public static function getValue($cate,$label)
    {
        $resD = Dict::where("cate",$cate)->where("label",$label)->value("value");
        if(!$resD){
            return 0;
        }
        return $resD;
    }

    /**
     * @param $cate
     * @param $v
     * @return mixed|string
     */
    public static function getLabel($cate,$v)
    {
        $resD = Dict::where("cate",$cate)->where("value",$v)->value("label");
        if(!$resD){
            return "";
        }
        return $resD;
    }

    /**
     * @param $cate
     * @param $data
     * @throws \Exception
     * 站点覆盖添加
     */
    public static function coverSiteAdd($cate,$data)
    {
        Dict::where("cate",$cate)->delete();
        $lists = [];
        foreach ($data as $k => $v){
            $lists[] = ["cate"=>"site","label"=>$v["url_m"],"value"=>$v["prefix_m"],"expand"=>"mobile"];
            $lists[] = ["cate"=>"site","label"=>$v["url_pc"],"value"=>$v["prefix_pc"],"expand"=>"pc"];
        }
        Db::name("dict")->data($lists)->insertAll();
    }
}